package com.kirk.cts.ui.listener;

import java.awt.*;


/**
 * <p> AbstractVerifier.java </p>
 *
 * @version 1.0.0
 * @Author kirkliu
 * @Date 2020-08-29 22:58
 * @Description 抽象校验类
 * 处理显示出错提示,改变出错组件背景,通知Form窗体等
 */
public abstract class AbstractVerifier extends javax.swing.InputVerifier {
    /**
     * 校验监听接口
     */
    private VerifierListener listener = null;
    /**
     * 消息
     */
    protected String message = "";

    public AbstractVerifier(VerifierListener listener) {
        this.listener = listener;
    }

    public boolean verify(javax.swing.JComponent jComponent) {
        if (!this.doVerify(jComponent)) {
            //设置背景颜色
            jComponent.setBackground(Color.PINK);
            reportError(jComponent, message);
            return false;
        }
        jComponent.setForeground(Color.black);
        //设置背景颜色
        jComponent.setBackground(null);
        jComponent.setToolTipText("");
        if (listener != null) {
            listener.validData(jComponent);
        }
        return true;
    }

    /**
     * 验证失败
     */
    private void reportError(javax.swing.JComponent jComponent, String message) {
        jComponent.setForeground(Color.red);
        jComponent.setToolTipText(message);
        if (listener != null) {
            listener.invalidData(message, jComponent);
        }
    }

    /**
     * 具体校验规则有子类实现
     */
    protected abstract boolean doVerify(javax.swing.JComponent jComponent);

    /**
     *
     */
    public void setMessage(String message) {
        this.message = message;
    }

}
